﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Jzy.UI.Web.BaseCode
{
    public class HtmlPager
    {
        public static string Pager(int currentPage, int pageSize, int totalRecords, string urlPrefix)
        {
            return Pager(currentPage, pageSize, totalRecords, urlPrefix, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <param name="urlPrefix"></param>
        /// <param name="isPara">是否是参数的方式呈现如?pageno=3</param>
        /// <returns></returns>
        public static string Pager(int currentPage, int pageSize, int totalRecords, string urlPrefix,bool isPara)
        {
            //1, 10, 120, "/"
            int DISP_COUNT = 10;//显示页码的个数

            StringBuilder sb = new StringBuilder();

            sb.Append("<div class='pagination'>");
            sb.Append("<div class='pager'>");


            int totalPage = totalRecords / pageSize;//总页数
            if (totalRecords % pageSize != 0)
            {
                totalPage++;
            }

            if (totalPage == 1)
            {
                return "";
            }

            if (currentPage < 1)
            {
                currentPage = 1;
            }
            if (currentPage > totalPage)
            {
                currentPage = totalPage;
            }


            //上一页
            if (currentPage == 1)
            {
                sb.Append("<span class='page_start'>&lt;</span>");
            }
            else
            {
                sb.Append("<a class='page_prev' href='" + urlPrefix.TrimEnd('/') + "/" + (currentPage - 1) + "'>&lt;</a>");
            }

            if (isPara)
            {
                if (urlPrefix.Contains("?"))
                {
                    urlPrefix += "&pageno=";
                }
                else
                {
                    urlPrefix += "?pageno=";
                }
            }
            else
            {
                urlPrefix = urlPrefix.TrimEnd('/') + "/";                
            }

            //中间内容
            if (totalPage <= DISP_COUNT || currentPage <= DISP_COUNT - 4)
            {               

                for (int i = 1; i <= Math.Min(totalPage, DISP_COUNT); i++)
                {
                    if (i == currentPage)
                    {
                        sb.Append("<span class='page_cur'>" + i + "</span>");
                    }
                    else
                    {
                        
                        
                        sb.Append("<a href='" + urlPrefix + i + "'>" + i + "</a>");
                    }
                }
            }

            if (totalPage > DISP_COUNT)
            {
                if (currentPage <= DISP_COUNT - 4)
                {
                    sb.Append("<span class='page_break'>...</span>");
                }
                else
                {
                    for (int i = 1; i <= 2; i++)
                    {
                        sb.Append("<a href='" + urlPrefix + i + "'>" + i + "</a>");
                    }
                    sb.Append("<span class='page_break'>...</span>");

                    for (int i = currentPage - 2; i <= currentPage + 5; i++)
                    {
                        if (i == currentPage)
                        {
                            sb.Append("<span class='page_cur'>" + i + "</span>");
                        }
                        else
                        {
                            sb.Append("<a href='" + urlPrefix + i + "'>" + i + "</a>");
                        }
                    }
                    sb.Append("<span class='page_break'>...</span>");

                }
            }

            //下一页

            if (currentPage == totalPage)
            {
                sb.Append("<span class='page_end'>下一页&nbsp;&gt;</span>");
            }
            else
            {
                sb.Append("<a class='page_next' href='" + urlPrefix + (currentPage + 1) + "'>下一页&nbsp;&gt;</a>");
            }

            sb.Append("</div>");
            sb.Append("</div>");

            return sb.ToString();
        }
    }
}
